"""
消息通知组件，实现类似Element Plus的消息提示和对话框功能
"""
from PyQt5.QtWidgets import (QLabel, QDialog, QVBoxLayout, QHBoxLayout, 
                            QPushButton, QWidget, QApplication, QGraphicsOpacityEffect,
                            QFrame, QGridLayout)
from PyQt5.QtCore import Qt, QTimer, QPropertyAnimation, QPoint, QSize, pyqtSignal, QEasingCurve, QRect
from PyQt5.QtGui import QIcon, QPixmap, QFont, QColor, QPainter, QPen
import os
from component.theme import Theme
from component.button import Button


class Notification(QFrame):
    """
    通知组件，用于显示从右侧滑出的通知
    
    用法:
        Notification.info(parent, "标题", "这是一条信息")
        Notification.success(parent, "标题", "操作成功")
        Notification.warning(parent, "标题", "警告信息")
        Notification.error(parent, "标题", "错误信息")
    """
    
    _instances = []  # 保存所有实例，用于管理多个通知
    
    def __init__(self, parent, title, message, type="info", duration=3000, closable=True):
        """
        初始化通知
        
        Args:
            parent: 父组件
            title: 通知标题
            message: 通知内容
            type: 通知类型，可选值为 info, success, warning, error
            duration: 显示时长，单位毫秒，0表示不自动关闭
            closable: 是否可关闭
        """
        super().__init__(parent)
        
        self.setObjectName("notification")
        self.setProperty("type", type)
        self.setMinimumWidth(330)
        self.setMaximumWidth(330)
        self.setMinimumHeight(100)
        self.setMaximumHeight(200)
        
        # 设置样式
        self.setStyleSheet(self._get_style_sheet(type))
        
        # 创建布局
        layout = QVBoxLayout(self)
        layout.setContentsMargins(16, 12, 16, 12)  # 调整内边距
        layout.setSpacing(8)
        
        # 标题和关闭按钮
        title_layout = QHBoxLayout()
        title_layout.setSpacing(8)
        
        # 图标
        icon_label = QLabel()
        icon_label.setFixedSize(20, 20)
        icon_pixmap = self._get_icon_pixmap(type)
        if icon_pixmap:
            icon_label.setPixmap(icon_pixmap)
        title_layout.addWidget(icon_label)
        
        # 标题
        title_label = QLabel(title)
        title_label.setObjectName("title")
        title_label.setFont(QFont(Theme.FONT_FAMILY, Theme.FONT_SIZE_MEDIUM, QFont.Bold))
        title_layout.addWidget(title_label)
        
        title_layout.addStretch()
        
        # 关闭按钮
        if closable:
            close_button = QPushButton("×")
            close_button.setObjectName("close-button")
            close_button.setFixedSize(20, 20)
            close_button.setCursor(Qt.PointingHandCursor)
            close_button.clicked.connect(self.close_with_animation)
            title_layout.addWidget(close_button)
        
        layout.addLayout(title_layout)
        
        # 消息内容
        message_label = QLabel(message)
        message_label.setObjectName("message")
        message_label.setWordWrap(True)
        message_label.setFont(QFont(Theme.FONT_FAMILY, Theme.FONT_SIZE_BASE))
        message_label.setContentsMargins(0, 4, 0, 4)  # 为消息内容添加额外的上下边距
        layout.addWidget(message_label)
        
        # 设置透明度效果
        self.opacity_effect = QGraphicsOpacityEffect(self)
        self.opacity_effect.setOpacity(0.0)  # 初始透明度为0
        self.setGraphicsEffect(self.opacity_effect)
        
        # 添加到实例列表
        Notification._instances.append(self)
        
        # 调整所有通知的位置
        self._adjust_positions()
        
        # 设置初始位置（在屏幕右侧外）
        parent_rect = self.parent().rect()
        self.move(parent_rect.width(), 20)
        
        # 显示通知
        self.show()
        
        # 创建位置动画
        self.pos_animation = QPropertyAnimation(self, b"pos")
        self.pos_animation.setDuration(400)
        self.pos_animation.setEasingCurve(QEasingCurve.OutCubic)
        
        # 创建透明度动画
        self.opacity_animation = QPropertyAnimation(self.opacity_effect, b"opacity")
        self.opacity_animation.setDuration(400)
        self.opacity_animation.setStartValue(0.0)
        self.opacity_animation.setEndValue(1.0)
        
        # 设置位置动画起始和结束位置
        start_pos = QPoint(parent_rect.width(), self.y())
        end_pos = QPoint(parent_rect.width() - self.width() - 20, self.y())
        
        self.pos_animation.setStartValue(start_pos)
        self.pos_animation.setEndValue(end_pos)
        
        # 开始动画
        self.pos_animation.start()
        self.opacity_animation.start()
        
        # 设置定时关闭
        if duration > 0:
            QTimer.singleShot(duration, self.close_with_animation)
    
    def _create_shadow_effect(self):
        """创建阴影效果"""
        shadow = QGraphicsOpacityEffect(self)
        shadow.setOpacity(1.0)
        return shadow
    
    def _get_style_sheet(self, type):
        """获取样式表"""
        base_style = f"""
            QFrame#notification {{
                background-color: {Theme.BG_COLOR};
                border-radius: {Theme.BORDER_RADIUS_BASE}px;
                border: 1px solid {Theme.BORDER_LIGHT};
                box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16);
            }}
            
            QLabel#title {{
                color: {Theme.TEXT_PRIMARY};
            }}
            
            QLabel#message {{
                color: {Theme.TEXT_REGULAR};
            }}
            
            QPushButton#close-button {{
                background-color: transparent;
                border: none;
                color: {Theme.TEXT_SECONDARY};
                font-size: 16px;
            }}
            
            QPushButton#close-button:hover {{
                color: {Theme.TEXT_PRIMARY};
            }}
        """
        
        type_styles = {
            "info": f"""
                QFrame#notification {{
                    border-left: 4px solid {Theme.INFO};
                }}
            """,
            "success": f"""
                QFrame#notification {{
                    border-left: 4px solid {Theme.SUCCESS};
                }}
            """,
            "warning": f"""
                QFrame#notification {{
                    border-left: 4px solid {Theme.WARNING};
                }}
            """,
            "error": f"""
                QFrame#notification {{
                    border-left: 4px solid {Theme.DANGER};
                }}
            """
        }
        
        return base_style + type_styles.get(type, "")
    
    def _get_icon_pixmap(self, type):
        """获取图标"""
        return create_icon_pixmap(type, 20)  # 使用较小的图标尺寸
    
    def _adjust_positions(self):
        """调整所有通知的位置"""
        parent_rect = self.parent().rect()
        
        # 计算通知的位置
        for i, instance in enumerate(Notification._instances):
            if instance.isVisible():
                x = parent_rect.width() - instance.width() - 20
                y = 20 + i * (instance.height() + 10)
                
                # 如果不是当前实例（正在创建的通知），则添加动画效果
                if instance != self:
                    # 创建位置动画
                    pos_animation = QPropertyAnimation(instance, b"pos")
                    pos_animation.setDuration(300)
                    pos_animation.setEasingCurve(QEasingCurve.OutCubic)
                    
                    # 设置动画起始和结束位置
                    start_pos = instance.pos()
                    end_pos = QPoint(x, y)
                    
                    pos_animation.setStartValue(start_pos)
                    pos_animation.setEndValue(end_pos)
                    
                    # 开始动画
                    pos_animation.start()
                else:
                    # 如果是当前实例，且动画正在进行，则更新动画的结束位置
                    if hasattr(instance, 'pos_animation') and instance.pos_animation.state() == QPropertyAnimation.Running:
                        instance.pos_animation.setEndValue(QPoint(x, y))
    
    def close_with_animation(self):
        """带动画效果的关闭"""
        # 创建关闭位置动画
        self.close_pos_animation = QPropertyAnimation(self, b"pos")
        self.close_pos_animation.setDuration(300)
        self.close_pos_animation.setEasingCurve(QEasingCurve.InCubic)
        
        # 创建关闭透明度动画
        self.close_opacity_animation = QPropertyAnimation(self.opacity_effect, b"opacity")
        self.close_opacity_animation.setDuration(300)
        self.close_opacity_animation.setStartValue(1.0)
        self.close_opacity_animation.setEndValue(0.0)
        
        # 设置位置动画起始和结束位置
        parent_rect = self.parent().rect()
        start_pos = self.pos()
        end_pos = QPoint(parent_rect.width(), self.y())
        
        self.close_pos_animation.setStartValue(start_pos)
        self.close_pos_animation.setEndValue(end_pos)
        
        # 动画结束后关闭通知
        self.close_opacity_animation.finished.connect(self.close)
        
        # 开始动画
        self.close_pos_animation.start()
        self.close_opacity_animation.start()
    
    def closeEvent(self, event):
        """关闭事件"""
        # 从实例列表中移除
        if self in Notification._instances:
            Notification._instances.remove(self)
        
        # 调整其他通知的位置
        for instance in Notification._instances:
            instance._adjust_positions()
        
        super().closeEvent(event)
    
    @classmethod
    def info(cls, parent, title, message, duration=3000, closable=True):
        """显示信息通知"""
        return cls(parent, title, message, "info", duration, closable)
    
    @classmethod
    def success(cls, parent, title, message, duration=3000, closable=True):
        """显示成功通知"""
        return cls(parent, title, message, "success", duration, closable)
    
    @classmethod
    def warning(cls, parent, title, message, duration=3000, closable=True):
        """显示警告通知"""
        return cls(parent, title, message, "warning", duration, closable)
    
    @classmethod
    def error(cls, parent, title, message, duration=3000, closable=True):
        """显示错误通知"""
        return cls(parent, title, message, "error", duration, closable)


class Message(QLabel):
    """
    消息提示组件，用于显示简短的提示信息
    
    用法:
        Message.info(parent, "这是一条信息")
        Message.success(parent, "操作成功")
        Message.warning(parent, "警告信息")
        Message.error(parent, "错误信息")
    """
    
    _instances = []  # 保存所有实例，用于管理多个消息
    
    def __init__(self, parent, message, type="info", duration=3000, closable=True):
        """
        初始化消息提示
        
        Args:
            parent: 父组件
            message: 消息内容
            type: 消息类型，可选值为 info, success, warning, error
            duration: 显示时长，单位毫秒，0表示不自动关闭
            closable: 是否可关闭
        """
        super().__init__(parent)
        
        self.setObjectName("message")
        self.setProperty("type", type)
        self.setText(message)
        self.setAlignment(Qt.AlignCenter)
        self.setStyleSheet(Theme.get_style_sheet("message"))
        self.setWordWrap(True)
        self.setMinimumWidth(300)
        self.setMaximumWidth(500)
        self.setMinimumHeight(60)  # 增加最小高度
        
        # 设置内边距 - 使用更合理的值
        self.setContentsMargins(20, 10, 20, 10)
        
        # 设置字体
        font = QFont(Theme.FONT_FAMILY, Theme.FONT_SIZE_BASE)
        font.setBold(True)
        self.setFont(font)
        
        # 设置阴影效果
        self.shadow = QGraphicsOpacityEffect(self)
        self.shadow.setOpacity(0.0)  # 初始透明度为0
        self.setGraphicsEffect(self.shadow)
        
        # 添加到实例列表
        Message._instances.append(self)
        
        # 调整所有消息的位置
        self._adjust_positions()
        
        # 设置初始位置（在屏幕顶部外）
        parent_rect = self.parent().rect()
        x = (parent_rect.width() - self.width()) // 2
        y = -self.height()
        self.move(x, y)
        
        # 显示消息
        self.show()
        
        # 创建位置动画
        self.pos_animation = QPropertyAnimation(self, b"pos")
        self.pos_animation.setDuration(300)
        self.pos_animation.setEasingCurve(QEasingCurve.OutCubic)
        
        # 创建透明度动画
        self.opacity_animation = QPropertyAnimation(self.shadow, b"opacity")
        self.opacity_animation.setDuration(300)
        self.opacity_animation.setStartValue(0.0)
        self.opacity_animation.setEndValue(1.0)
        self.opacity_animation.start()
        
        # 设置位置动画起始和结束位置
        start_pos = QPoint(x, y)
        end_y = 20 + len(Message._instances) * (self.height() + 10)
        end_pos = QPoint(x, end_y)
        
        self.pos_animation.setStartValue(start_pos)
        self.pos_animation.setEndValue(end_pos)
        
        # 开始动画
        self.pos_animation.start()
        
        # 设置定时关闭
        if duration > 0:
            QTimer.singleShot(duration, self.close_with_animation)
    
    def _create_shadow_effect(self):
        """创建阴影效果"""
        shadow = QGraphicsOpacityEffect(self)
        shadow.setOpacity(0.9)
        return shadow
    
    def _adjust_positions(self):
        """调整所有消息的位置"""
        parent_rect = self.parent().rect()
        
        # 计算消息的位置
        for i, instance in enumerate(Message._instances):
            if instance.isVisible():
                x = (parent_rect.width() - instance.width()) // 2
                y = 20 + i * (instance.height() + 10)
                
                # 如果不是当前实例（正在创建的消息），则添加动画效果
                if instance != self:
                    # 创建位置动画
                    pos_animation = QPropertyAnimation(instance, b"pos")
                    pos_animation.setDuration(300)
                    pos_animation.setEasingCurve(QEasingCurve.OutCubic)
                    
                    # 设置动画起始和结束位置
                    start_pos = instance.pos()
                    end_pos = QPoint(x, y)
                    
                    pos_animation.setStartValue(start_pos)
                    pos_animation.setEndValue(end_pos)
                    
                    # 开始动画
                    pos_animation.start()
                else:
                    # 如果是当前实例，且动画正在进行，则更新动画的结束位置
                    if hasattr(instance, 'pos_animation') and instance.pos_animation.state() == QPropertyAnimation.Running:
                        instance.pos_animation.setEndValue(QPoint(x, y))
    
    def close_with_animation(self):
        """带动画效果的关闭"""
        # 创建关闭位置动画
        self.close_pos_animation = QPropertyAnimation(self, b"pos")
        self.close_pos_animation.setDuration(300)
        self.close_pos_animation.setEasingCurve(QEasingCurve.InCubic)
        
        # 创建关闭透明度动画
        self.close_opacity_animation = QPropertyAnimation(self.shadow, b"opacity")
        self.close_opacity_animation.setDuration(300)
        self.close_opacity_animation.setStartValue(1.0)
        self.close_opacity_animation.setEndValue(0.0)
        
        # 设置位置动画起始和结束位置
        start_pos = self.pos()
        end_pos = QPoint(start_pos.x(), -self.height())
        self.close_pos_animation.setStartValue(start_pos)
        self.close_pos_animation.setEndValue(end_pos)
        
        # 动画结束后关闭消息
        self.close_opacity_animation.finished.connect(self.close)
        
        # 开始动画
        self.close_pos_animation.start()
        self.close_opacity_animation.start()
    
    def closeEvent(self, event):
        """关闭事件"""
        # 从实例列表中移除
        if self in Message._instances:
            Message._instances.remove(self)
        
        # 调整其他消息的位置
        for instance in Message._instances:
            instance._adjust_positions()
        
        super().closeEvent(event)
    
    @classmethod
    def info(cls, parent, message, duration=3000, closable=True):
        """显示信息消息"""
        return cls(parent, message, "info", duration, closable)
    
    @classmethod
    def success(cls, parent, message, duration=3000, closable=True):
        """显示成功消息"""
        return cls(parent, message, "success", duration, closable)
    
    @classmethod
    def warning(cls, parent, message, duration=3000, closable=True):
        """显示警告消息"""
        return cls(parent, message, "warning", duration, closable)
    
    @classmethod
    def error(cls, parent, message, duration=3000, closable=True):
        """显示错误消息"""
        return cls(parent, message, "error", duration, closable)


class MessageBox(QDialog):
    """
    消息对话框组件，用于显示需要用户确认的消息
    
    用法:
        result = MessageBox.confirm(parent, "确认", "确定要删除吗？")
        if result:
            # 用户点击了确认
            pass
        
        MessageBox.alert(parent, "提示", "操作已完成")
    """
    
    def __init__(self, parent, title, message, type="info", 
                 confirm_text="确定", cancel_text="取消", 
                 confirm_type="primary", cancel_type="info"):
        """
        初始化消息对话框
        
        Args:
            parent: 父组件
            title: 标题
            message: 消息内容
            type: 消息类型，可选值为 info, success, warning, error
            confirm_text: 确认按钮文本
            cancel_text: 取消按钮文本
            confirm_type: 确认按钮类型
            cancel_type: 取消按钮类型
        """
        super().__init__(parent)
        
        self.setWindowTitle(title)
        self.setWindowFlags(Qt.Dialog | Qt.FramelessWindowHint)
        self.setModal(True)
        self.setStyleSheet(Theme.get_style_sheet("message_box"))
        self.setMinimumWidth(400)
        self.setProperty("type", type)  # 设置类型属性，用于样式表
        
        # 创建布局
        layout = QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        
        # 标题栏
        title_bar = QWidget(self)
        title_bar.setObjectName("title_bar")
        title_bar_layout = QHBoxLayout(title_bar)
        title_bar_layout.setContentsMargins(20, 16, 20, 16)
        
        # 标题
        title_label = QLabel(title, self)
        title_label.setObjectName("title")
        title_bar_layout.addWidget(title_label)
        
        # 关闭按钮
        close_button = QPushButton("×", self)
        close_button.setObjectName("close-button")
        close_button.setFixedSize(20, 20)
        close_button.setCursor(Qt.PointingHandCursor)
        close_button.clicked.connect(self.reject)
        title_bar_layout.addWidget(close_button)
        
        layout.addWidget(title_bar)
        
        # 内容区域
        content_widget = QWidget(self)
        content_widget.setObjectName("content_widget")
        content_layout = QHBoxLayout(content_widget)
        content_layout.setContentsMargins(20, 20, 20, 20)
        content_layout.setSpacing(15)
        
        # 图标
        icon_label = QLabel(self)
        icon_label.setObjectName("icon")
        icon_label.setFixedSize(32, 32)
        icon_pixmap = self._get_icon_pixmap(type)
        if icon_pixmap:
            icon_label.setPixmap(icon_pixmap)
        content_layout.addWidget(icon_label)
        
        # 消息内容
        content_label = QLabel(message, self)
        content_label.setObjectName("content")
        content_label.setWordWrap(True)
        content_layout.addWidget(content_label, 1)
        
        layout.addWidget(content_widget, 1)
        
        # 按钮容器
        button_container = QWidget(self)
        button_container.setObjectName("button_container")
        button_layout = QHBoxLayout(button_container)
        button_layout.setContentsMargins(16, 12, 16, 12)
        button_layout.setSpacing(10)
        
        # 添加按钮
        button_layout.addStretch()
        
        # 取消按钮
        if cancel_text:
            self.cancel_button = Button(cancel_text, self, type=cancel_type)
            self.cancel_button.setObjectName("cancel_button")
            self.cancel_button.clicked.connect(self.reject)
            button_layout.addWidget(self.cancel_button)
        
        # 确认按钮
        self.confirm_button = Button(confirm_text, self, type=confirm_type)
        self.confirm_button.setObjectName("confirm_button")
        self.confirm_button.clicked.connect(self.accept)
        button_layout.addWidget(self.confirm_button)
        
        layout.addWidget(button_container)
        
        # 设置透明度和缩放效果
        self.opacity_effect = QGraphicsOpacityEffect(self)
        self.opacity_effect.setOpacity(0.0)
        self.setGraphicsEffect(self.opacity_effect)
        
        # 初始大小
        self.initial_geometry = self.geometry()
    
    def _get_icon_pixmap(self, type):
        """获取图标"""
        return create_icon_pixmap(type, 20)  # 使用较小的图标尺寸
    
    def showEvent(self, event):
        """显示事件，添加动画效果"""
        super().showEvent(event)
        
        # 创建透明度动画
        self.opacity_animation = QPropertyAnimation(self.opacity_effect, b"opacity")
        self.opacity_animation.setDuration(250)
        self.opacity_animation.setStartValue(0.0)
        self.opacity_animation.setEndValue(1.0)
        self.opacity_animation.start()
        
        # 创建缩放动画
        self.geometry_animation = QPropertyAnimation(self, b"geometry")
        self.geometry_animation.setDuration(250)
        self.geometry_animation.setEasingCurve(QEasingCurve.OutCubic)
        
        # 计算起始和结束几何
        center = self.geometry().center()
        start_geometry = QRect(
            center.x() - self.width() * 0.4,
            center.y() - self.height() * 0.4,
            self.width() * 0.8,
            self.height() * 0.8
        )
        end_geometry = self.geometry()
        
        self.geometry_animation.setStartValue(start_geometry)
        self.geometry_animation.setEndValue(end_geometry)
        self.geometry_animation.start()
    
    def done(self, result):
        """完成对话框"""
        # 保存结果
        self._result = result
        
        # 如果动画正在运行，等待动画完成后再返回结果
        if hasattr(self, 'close_opacity_animation') and self.close_opacity_animation.state() == QPropertyAnimation.Running:
            # 动画已经在运行，不需要再次触发closeEvent
            self.close_opacity_animation.finished.connect(lambda: QDialog.done(self, self._result))
        else:
            # 触发关闭动画，然后在动画完成后调用QDialog.done
            self._trigger_close_animation()
            self.close_opacity_animation.finished.connect(lambda: QDialog.done(self, self._result))
    
    def _trigger_close_animation(self):
        """触发关闭动画"""
        # 创建关闭透明度动画
        self.close_opacity_animation = QPropertyAnimation(self.opacity_effect, b"opacity")
        self.close_opacity_animation.setDuration(200)
        self.close_opacity_animation.setStartValue(1.0)
        self.close_opacity_animation.setEndValue(0.0)
        
        # 创建关闭缩放动画
        self.close_geometry_animation = QPropertyAnimation(self, b"geometry")
        self.close_geometry_animation.setDuration(200)
        self.close_geometry_animation.setEasingCurve(QEasingCurve.InCubic)
        
        # 计算起始和结束几何
        center = self.geometry().center()
        start_geometry = self.geometry()
        end_geometry = QRect(
            center.x() - self.width() * 0.4,
            center.y() - self.height() * 0.4,
            self.width() * 0.8,
            self.height() * 0.8
        )
        
        self.close_geometry_animation.setStartValue(start_geometry)
        self.close_geometry_animation.setEndValue(end_geometry)
        
        # 开始动画
        self.close_opacity_animation.start()
        self.close_geometry_animation.start()
    
    def closeEvent(self, event):
        """关闭事件，添加动画效果"""
        # 如果已经有关闭动画在运行，直接接受事件
        if hasattr(self, 'close_opacity_animation') and self.close_opacity_animation.state() == QPropertyAnimation.Running:
            event.accept()
            return
        
        # 阻止默认关闭行为
        event.ignore()
        
        # 触发关闭动画
        self._trigger_close_animation()
        
        # 保存事件引用，以便在动画结束后使用
        self._close_event = event
        
        # 动画结束后真正关闭对话框
        self.close_opacity_animation.finished.connect(self._finish_close)
    
    def _finish_close(self):
        """完成关闭操作"""
        # 使用保存的事件调用父类的closeEvent
        if hasattr(self, '_close_event'):
            QDialog.closeEvent(self, self._close_event)
    
    def accept(self):
        """接受对话框"""
        self.done(QDialog.Accepted)
    
    def reject(self):
        """拒绝对话框"""
        self.done(QDialog.Rejected)
    
    @classmethod
    def confirm(cls, parent, title, message, confirm_text="确定", cancel_text="取消"):
        """
        显示确认对话框
        
        Args:
            parent: 父组件
            title: 标题
            message: 消息内容
            confirm_text: 确认按钮文本
            cancel_text: 取消按钮文本
        
        Returns:
            bool: 用户是否点击了确认按钮
        """
        dialog = cls(parent, title, message, "warning", 
                    confirm_text, cancel_text, "primary", "info")
        return dialog.exec_() == QDialog.Accepted
    
    @classmethod
    def alert(cls, parent, title, message, confirm_text="确定"):
        """
        显示提示对话框
        
        Args:
            parent: 父组件
            title: 标题
            message: 消息内容
            confirm_text: 确认按钮文本
        """
        dialog = cls(parent, title, message, "info", 
                    confirm_text, None, "primary", None)
        dialog.exec_()
    
    @classmethod
    def info(cls, parent, title, message, confirm_text="确定", cancel_text="取消"):
        """显示信息对话框"""
        dialog = cls(parent, title, message, "info", 
                    confirm_text, cancel_text, "primary", "info")
        return dialog.exec_() == QDialog.Accepted
    
    @classmethod
    def success(cls, parent, title, message, confirm_text="确定", cancel_text="取消"):
        """显示成功对话框"""
        dialog = cls(parent, title, message, "success", 
                    confirm_text, cancel_text, "success", "info")
        return dialog.exec_() == QDialog.Accepted
    
    @classmethod
    def warning(cls, parent, title, message, confirm_text="确定", cancel_text="取消"):
        """显示警告对话框"""
        dialog = cls(parent, title, message, "warning", 
                    confirm_text, cancel_text, "warning", "info")
        return dialog.exec_() == QDialog.Accepted
    
    @classmethod
    def error(cls, parent, title, message, confirm_text="确定", cancel_text="取消"):
        """显示错误对话框"""
        dialog = cls(parent, title, message, "error", 
                    confirm_text, cancel_text, "danger", "info")
        return dialog.exec_() == QDialog.Accepted


# 通用函数：创建图标
def create_icon_pixmap(type, size=32):
    """
    创建一个类型特定的图标
    
    Args:
        type: 图标类型，可选值为 info, success, warning, error
        size: 图标大小
    
    Returns:
        QPixmap: 创建的图标
    """
    # 创建一个空白的图标
    pixmap = QPixmap(size, size)
    pixmap.fill(Qt.transparent)
    
    # 创建画家
    painter = QPainter(pixmap)
    painter.setRenderHint(QPainter.Antialiasing)
    
    # 根据类型设置颜色
    if type == "info":
        color = QColor(Theme.INFO)
    elif type == "success":
        color = QColor(Theme.SUCCESS)
    elif type == "warning":
        color = QColor(Theme.WARNING)
    elif type == "error":
        color = QColor(Theme.DANGER)
    else:
        color = QColor(Theme.INFO)
    
    # 绘制图标
    painter.setPen(Qt.NoPen)
    painter.setBrush(QColor(color))
    
    center = size // 2
    radius = size // 4
    
    # 绘制不同形状的图标
    if type == "info":
        # 绘制信息图标（圆形加感叹号）
        painter.drawEllipse(center - radius, center - radius, radius * 2, radius * 2)
        painter.setPen(QColor(Theme.BG_COLOR))
        painter.setFont(QFont(Theme.FONT_FAMILY, size // 3, QFont.Bold))
        painter.drawText(QRect(center - radius, center - radius, radius * 2, radius * 2), Qt.AlignCenter, "i")
    elif type == "success":
        # 绘制成功图标（对勾）
        painter.drawEllipse(center - radius, center - radius, radius * 2, radius * 2)
        painter.setPen(QPen(QColor(Theme.BG_COLOR), size // 16))
        painter.drawLine(center - radius // 2, center, center, center + radius // 2)
        painter.drawLine(center, center + radius // 2, center + radius, center - radius // 2)
    elif type == "warning":
        # 绘制警告图标（三角形加感叹号）
        points = [
            QPoint(center, center - radius),
            QPoint(center + radius, center + radius),
            QPoint(center - radius, center + radius)
        ]
        painter.drawPolygon(points)
        painter.setPen(QColor(Theme.BG_COLOR))
        painter.setFont(QFont(Theme.FONT_FAMILY, size // 3, QFont.Bold))
        painter.drawText(QRect(center - radius, center - radius // 2, radius * 2, radius * 2), Qt.AlignCenter, "!")
    elif type == "error":
        # 绘制错误图标（圆形加叉号）
        painter.drawEllipse(center - radius, center - radius, radius * 2, radius * 2)
        painter.setPen(QPen(QColor(Theme.BG_COLOR), size // 16))
        painter.drawLine(center - radius // 2, center - radius // 2, center + radius // 2, center + radius // 2)
        painter.drawLine(center + radius // 2, center - radius // 2, center - radius // 2, center + radius // 2)
    
    painter.end()
    return pixmap 